package wc.parserUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParserSentence extends Parser {
	
	private List<String> _sentence = new ArrayList<String>();

    static private Pattern _pSentence = Pattern.compile("[\\.\\!\\?]((\\s[A-ZА-Я\\d]?)|$)");
    static private Pattern _pTail = Pattern.compile("[a-zA-Zа-яА-Я\\d]+");

	public ParserSentence() {}
	
	public void parseLines(List<String> lines, boolean clear) {
		if(clear) clear();

		String buffer = "";
        for(String line : lines) {
        	line = line.toLowerCase();
            line = line.replace("ё", "е");
            Matcher m = _pSentence.matcher(line);
            int start = 0;
            int end;
            while(m.find()) {
                end = m.start();
                if(start == 0) {
                    _sentence.add(buffer + " " + line.substring(start, end));
                    buffer = "";
                } else {
                    _sentence.add(line.substring(start, end));
                }
                start = end;
            }
            if(_pTail.matcher(line).find(start)) {
                buffer = line.substring(start + 1);
            }
        }
	}
	
	public List<String> getSentence() {
		return _sentence;
	}
	
	protected void clear() {
		_sentence.clear();
	}
	
}
